/*
  Copyright (c) 2010
  The Regents of the University of Michigan
  All Rights Reserved

  Permission is granted to use, copy, create derivative works, and
  redistribute this software and such derivative works for any purpose,
  so long as the name of the University of Michigan is not used in
  any advertising or publicity pertaining to the use or distribution
  of this software without specific, written prior authorization. If
  the above copyright notice or any other identification of the
  University of Michigan is included in any copy of any portion of
  this software, then the disclaimer below must also be included.

  This software is provided as is, without representation or warranty
  of any kind either express or implied, including without limitation
  the implied warranties of merchantability, fitness for a particular
  purpose, or noninfringement.  The Regents of the University of
  Michigan shall not be liable for any damages, including special,
  indirect, incidental, or consequential damages, with respect to any
  claim arising out of or in connection with the use of the software,
  even if it has been or is hereafter advised of the possibility of
  such damages.
*/
#include "clog_parser.h"

#include <ctype.h>
#include <glib.h>
#include <glob.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Map keys to count of peer records under that key. */
GHashTable *torrent_keys;

guint key_hash(gconstpointer kk) {
  /* key is a SHA-1, just use first few bytes */
  guint ret;
  memcpy(&ret, kk, sizeof ret);
  return ret;
}

gboolean key_equal(gconstpointer k1, gconstpointer k2) {
  return memcmp(k1, k2, KEY_SIZE) == 0;
}

gboolean str_isdigit(const char *str, int len) {
  if (!len) {
    return FALSE;
  }

  int ii;
  for (ii = 0; ii != len; ++ii) {
    if (!isdigit(str[ii])) {
      return FALSE;
    }
  }

  return TRUE;
}

gboolean is_peer_record(const char *str, int len) {
  if (len > 29) {
    return FALSE;
  }

  if (len <= 5 && str_isdigit(str, len)) {
    return TRUE;
  }

  /* TODO: the full test. */
}

void build_keys_to_peer_records_map(struct LogRecord *rec, void *junk) {
  if (!is_peer_record(rec->value, rec->value_len)) {
    return;
  }
  gpointer value = g_hash_table_lookup(torrent_keys, rec->key);
  if (!value) {
    g_hash_table_insert(torrent_keys, g_memdup(rec->key, KEY_SIZE), GINT_TO_POINTER(0));
  } else {
    int count = GPOINTER_TO_INT(value);
    g_hash_table_insert(torrent_keys, rec->key, GINT_TO_POINTER(count + 1));
  }
}

int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s log_pattern\n", argv[0]);
    return 1;
  }

  glob_t glob_results = {0};
  if (glob(argv[1], GLOB_NOSORT | GLOB_TILDE, NULL, &glob_results) != 0) {
    fprintf(stderr, "Glob error!\n");
    return 1;
  }

  torrent_keys = g_hash_table_new_full(key_hash, key_equal, g_free, NULL);
  int ii;
  for (ii = 0; ii != glob_results.gl_pathc; ++ii) {
    log_parser_for_each(glob_results.gl_pathv[ii], 1,
                        build_keys_to_peer_records_map, NULL);
  }

  printf("Found %d possible DHT-tracked torrents\n", g_hash_table_size(torrent_keys));
  return 0;
}
